package 实验六;

interface Component {
    void display(); // 显示组件的方法
}

abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void display() {
        component.display();
    }
}

class TextDisplay implements Component {
    private String text;

    public TextDisplay(String text) {
        this.text = text;
    }

    @Override
    public void display() {
        System.out.println("文本: " + text);
    }
}

class BorderDecorator extends Decorator {

    public BorderDecorator(Component component) {
        super(component);
    }

    @Override
    public void display() {
        System.out.println("边框开始");
        super.display();
        System.out.println("边框结束");
    }
}

class ScrollDecorator extends Decorator {

    public ScrollDecorator(Component component) {
        super(component);
    }

    @Override
    public void display() {
        System.out.println("滚动条启用");
        super.display();
        System.out.println("滚动条禁用");
    }
}

public class 装饰器模式 {
    public static void main(String[] args) {
        // 创建一个基本的文本显示组件
        Component textDisplay = new TextDisplay("Hello, World!");

        // 为文本显示组件添加边框
        Component borderedText = new BorderDecorator(textDisplay);

        // 再为带有边框的文本显示组件添加滚动条
        Component scrollableText = new ScrollDecorator(borderedText);

        // 显示最终的装饰后的组件
        scrollableText.display();
    }
}
